<template>
	<!-- 地址列表 -->
	<view class="addressList">
		<view v-if="addressListData.length > 0">
			<hwxcAddress v-for="(item, index) in addressListData" :key="item.id" :addressInfo="item" @refresh="getrefresh" />
		</view>

		<u-empty v-else mode="address" icon="http://cdn.uviewui.com/uview/empty/address.png"></u-empty>

		<view class="address-btn" :style="bottomStyle">
			<view class="btn" @click="gotopath">新增收货地址</view>
		</view>
	</view>
</template>

<script>
import { userAddressList } from '../../api/home/index.js';
import hwxcAddress from '../../components/hwxcAddress/hwxcAddress.vue';
export default {
	data() {
		return {
			// 地址列表数据
			addressListData: []
		};
	},

	components: {
		hwxcAddress
	},

	computed: {
		bottomStyle() {
			const systemInfo = uni.getSystemInfoSync();
			const { safeAreaInsets } = systemInfo;
			const bottomSafeAreaHeight = safeAreaInsets.bottom || 0;
			return `padding-bottom: ${bottomSafeAreaHeight ? bottomSafeAreaHeight + 'px' : '20rpx'}`;
		}
	},

	async onShow() {
		await this.getUserAddressList();
	},

	methods: {
		// 获取地址列表
		async getUserAddressList() {
			try {
				const resData = await userAddressList();
				this.addressListData = resData.data;
			} catch (e) {
				throw Error(e);
			}
		},

		getrefresh() {
			this.getUserAddressList();
		},

		gotopath() {
			uni.navigateTo({
				url: `/pages/addAddress/addAddress`
			});
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
}
</style>

<style scoped lang="scss">
.addressList {
	box-sizing: border-box;
	padding: 0px 30rpx;
	.address-btn {
		width: 100%;
		position: fixed;
		bottom: 0px;
		left: 0px;
		padding: 20rpx;
		padding-bottom: 0rpx;
		box-sizing: border-box;
		.btn {
			background-color: #e43927;
			color: #fff;
			border-radius: 10rpx;
			width: 100%;
			height: 80rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 28rpx;
		}
	}
}
</style>
